//
//  M2BannerView.m
//  m2048
//
//  Created by lcb on 5/5/15.
//  Copyright (c) 2015 lcb. All rights reserved.
//

#import "M2BannerView.h"
#import "BaiduMobAdDelegateProtocol.h"
#import "BaiduMobAdView.h"

@interface M2BannerView ()<BaiduMobAdViewDelegate>
{
    
}

@property (strong, nonatomic) BaiduMobAdView* sharedAdView;
@end
@implementation M2BannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self commonInit];
    }
    return self;
}
-(void)dealloc
{
    [self.sharedAdView close];
}
- (void)commonInit
{
    self.backgroundColor = [UIColor clearColor];
    //使用嵌入广告的方法实例。
    self.sharedAdView = [[BaiduMobAdView alloc] initWithFrame:self.bounds];
    //sharedAdView.AdUnitTag = @"myAdPlaceId1";
    //此处为广告位id，可以不进行设置，如需设置，在百度移动联盟上设置广告位id，然后将得到的id填写到此处。
    self.sharedAdView.AdType = BaiduMobAdViewTypeBanner;
    self.sharedAdView.delegate = self;
    [self addSubview:self.sharedAdView];
    [self.sharedAdView start];
}
- (NSString *)publisherId
{
    return @"ce7235ab";
}

- (NSString*) appSpec
{
    return @"ce7235ab";
}


-(BOOL) enableLocation
{
    //启用location会有一次alert提示
    return NO;
}


-(void) willDisplayAd:(BaiduMobAdView*) adview
{
    //在广告即将展示时，产生一个动画，把广告条加载到视图中
    self.sharedAdView.hidden = NO;
    CGRect f = self.sharedAdView.frame;
    f.origin.x = -320;
    self.sharedAdView.frame = f;
    [UIView beginAnimations:nil context:nil];
    f.origin.x = 0;
    self.sharedAdView.frame = f;
    [UIView commitAnimations];
    NSLog(@"delegate: will display ad");
    
}

-(void) failedDisplayAd:(BaiduMobFailReason) reason;
{
    NSLog(@"delegate:---- failedDisplayAd %d", reason);
}
@end